package com.cpecc.controller.wuzi;

import com.cpecc.framework.util.DateUtils;
import com.cpecc.framework.util.MoneyUtils;
import com.cpecc.framework.web.APIResult;
import com.cpecc.liucheng.manager.HuanJieManager;
import com.cpecc.mail.SendMailThread;
import com.cpecc.manager.system.*;
import com.cpecc.manager.wuzi.*;
import com.cpecc.model.system.*;
import com.cpecc.model.wuzi.*;
import com.cpecc.vo.CaiGouDaYinVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.cpecc.framework.util.DateUtils.FORMAT_SHORT_CN;

@Controller
@RequestMapping(value = "/wuzi")
public class ShenQingController {
	
	@Autowired
	private UserManager userManager;
	
	@Autowired
	private ShenQingManager shenQingManager;
	
	@Autowired
	private ShenQingJLManager shenQingJLManager;
	
	@Autowired
	private HuanJieManager huanJieManager;
	
	@Autowired
	private WuZiLBManager wuZiLBManager;
	
	@Autowired
	private BiZhongManager biZhongManager;
	
	@Autowired
	private WuZiManager wuZiManager;
	
	@Autowired
	private CaiGouManager caiGouManager;
	
	@Autowired
	private HeTongManager heTongManager;
	
	@Autowired
	private ZhengMingManager zhengMingManager;
	
	@Autowired
	private DepManager depManager;
	
	@Autowired
	private FenTanManager fenTanManager;
	
	@Autowired
	private ShiYongZTManager shiYongZTManager;
	
	@Autowired
	private HuanHuoManager huanHuoManager;
	
	@Autowired
	private HuanHuoJLManager huanHuoJLManager;
	
	@Autowired
	private TuiHuoManager tuiHuoManager;
	
	@Autowired
	private CaiGouSPRManager caiGouSPRManager;
	
	@Autowired
	private BianMaManager bianMaManager;
	
	@Autowired
	private EmailManager emailManager;
	
	@Autowired
	private ToDoManager toDoManager;
	
	@Autowired
	private XiuGaiJLManager xiuGaiJLManager;
	
	@Autowired
	private YouJianManager youJianManager;
	
	@Autowired
	private RootManager rootManager;

	/**
	 * 跳转至申请流程不同意页面
	 * @param id
	 * @param currentHuanJieId
	 * @param nextHuanJieId
	 * @return
	 */
	@RequestMapping(value = "/toShenQingBTY")
	public ModelAndView toShenQingBTY(String id, Integer currentHuanJieId, Integer nextHuanJieId){
		ModelAndView mav=new ModelAndView();
		mav.addObject("id", id);
		mav.addObject("currentHuanJieId", currentHuanJieId);
		mav.addObject("nextHuanJieId", nextHuanJieId);
		mav.setViewName("shenqing/shenQingBTY");
		return mav;
	}

	/**
	 * 保存申请流程不同意内容
	 * @param id
	 * @param nextHuanJieId
	 * @param piFuNR
	 * @return
	 */
	@RequestMapping(value = "/saveShenQingBTY", method = RequestMethod.POST)
	@ResponseBody
	public APIResult saveShenQingBTY(String id, Integer nextHuanJieId, String piFuNR){
		ShenQing shenQing = shenQingManager.get(id);
		if (shenQing != null) {
			shenQing.setHuanJie(huanJieManager.get(nextHuanJieId));

			Subject currentUser = SecurityUtils.getSubject();
			Object obj=currentUser.getPrincipals();
			String name=obj.toString();
			User user=userManager.findByUsername(name);

			ShenQingJL shenQingJL=new ShenQingJL();
			shenQingJL.setShenQing(shenQing);
			shenQingJL.setPiFuLX("不同意");
			shenQingJL.setPiFuNR(piFuNR);
			shenQingJL.setCaoZuoRQ(shenQing.getShenQingRQ());
			shenQingJL.setHuanJie(huanJieManager.get(nextHuanJieId));
			shenQingJL.setUser(user);
			shenQingJLManager.save(shenQingJL);
		}
		return APIResult.success();
	}
	
	private List<User> getXiaYiBuR(String shenQingId){
		List<User> list=new ArrayList<User>();
		ShenQing shenQing=shenQingManager.get(shenQingId);
		int huanJie=shenQing.getHuanJie().getId();
		switch(huanJie){
			//部门审批
			case 102: list.addAll(caiGouSPRManager.getAllShenPiR(shenQingId)); break;
			//归口部门负责人审批
			case 103: list.addAll(userManager.getGKBMFZR()); break;
			//采买人采购
			case 104: list.add(shenQing.getCaiMaiR());break;

			//物资入库
			case 105:
			//物资发放
			case 106:
				list.addAll(userManager.getGZGLY());
				break;

			//使用人验收
			case 107: list.add(shenQing.getWuZi().getUser());break;
			//打印单据
			case 108: list.addAll(userManager.getGZGLY()); break;
			case 109: list.addAll(userManager.getAllCaiWuR()); break;

			//低值管理员选择采买人
			case 111:
			//采购换货
			case 202:
				list.add(shenQing.getCaiMaiR());
				break;

			//换货入库
			case 203: list.addAll(userManager.getGZGLY()); break;
			//采买人退货
			case 302: list.add(shenQing.getCaiMaiR()); break;
			//退货低值管理员审批
			case 303: list.addAll(userManager.getGZGLY()); break;
			default: break;
		}
		return list;
	}
	
	@RequestMapping(value = "/caiGouDaiBan")
	public ModelAndView caiGouDaiBan(){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);		
		ModelAndView mav=new ModelAndView();
		mav.addObject("caiGouLingYong", shenQingManager.getAllLingYong(user));

		if(currentUser.hasRole("部门负责人")){
			mav.addObject("buMenCGSP", caiGouSPRManager.getMySPList(user));

			if (userManager.isGKBMFZR(user)) {
				mav.addObject("caiGouXZCMR", shenQingManager.getAllCaiGouZT(103));
			}
		}
		if(currentUser.hasRole("低值管理员")){
			mav.addObject("yanShouRK", shenQingManager.getAllYanShouRK());

			mav.addObject("caiGouFF", shenQingManager.getAllCaiGouZT(106));

			mav.addObject("caiGouRZ", shenQingManager.getAllCaiGouZT(108));

			mav.addObject("tuiHuoQRList", tuiHuoManager.getAllTuiHuoSQ(303));
		}
		if(currentUser.hasRole("财务")){
			mav.addObject("caiGouCWYS", shenQingManager.getAllCaiGouZT(109));
		}
		mav.addObject("caiGouSP", shenQingManager.getAllCaiGouZT2CMR(104,user));
		mav.addObject("huanHuoList", huanHuoManager.getAllHuanHuoSQ2CMR(1,user));
		mav.addObject("tuiHuoCGList", tuiHuoManager.getAllTuiHuoSQ2CMR(302,user));
		mav.setViewName("shenqing/shenQingDaiBan");
		return mav;
	}
	
	@RequestMapping(value = "/getCaiGouShu")
	public void getCaiGouShu(HttpServletResponse response) {
		int zongShu = 0;
		Subject currentUser = SecurityUtils.getSubject();
		Object obj = currentUser.getPrincipals();
		String name = obj.toString();
		User user = userManager.findByUsername(name);
		if (currentUser.hasRole("部门负责人")) {
			zongShu += caiGouSPRManager.getMySPList(user).size();
			if (userManager.isGKBMFZR(user)) {
				zongShu+=shenQingManager.getAllCaiGouZT(103).size();
			}
		}
		if (currentUser.hasRole("低值管理员")) {
			zongShu += shenQingManager.getAllYanShouRK().size();
			zongShu += shenQingManager.getAllCaiGouZT(106).size();
			zongShu += shenQingManager.getAllCaiGouZT(108).size();
			zongShu += tuiHuoManager.getAllTuiHuoSQ(303).size();
		}
		if (currentUser.hasRole("财务")) {
			zongShu += shenQingManager.getAllCaiGouZT(109).size();
		}

		zongShu += shenQingManager.getAllCaiGouZT2CMR(104, user).size();
		zongShu += huanHuoManager.getAllHuanHuoSQ2CMR(1, user).size();
		zongShu += tuiHuoManager.getAllTuiHuoSQ2CMR(302, user).size();
		zongShu += shenQingManager.getAllLingYong(user).size();

		response.setContentType("application/json;charset=utf-8");
		String result = "{\"result\":\"(" + zongShu + ")\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 1.申请采购
	 */
	@RequestMapping(value = "/shenQingCaiGou")
	public ModelAndView shenQingCaiGou() {
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String status="display";
		if(user.getRole().getRoleName().equals("部门负责人")){
			if(user.getGangWei()!=null && user.getGangWei().equals("1")){
				mav.addObject("lingDaos", userManager.getBuMenLingDaos2(user));
			}else if(user.getGangWei()!=null && user.getGangWei().equals("2")){
				status="none";
			}
		}else{
			mav.addObject("lingDaos", userManager.getBuMenLingDaos(user));	
		}
		mav.addObject("status", status);
		mav.setViewName("shenqing/shenQingCaiGou");
		return mav;
	}
	
	@RequestMapping(value = "/shenQingCaiGouSave")
	public void shenQingCaiGouSave(HttpServletRequest request,HttpServletResponse response) {		
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		Dep dep=userManager.getUserDep(user);
		String[] type1 = request.getParameterValues("no");
		String shenQingYY=request.getParameter("shenQingYY");
		String wuZiMiaoShu=request.getParameter("wuZiMiaoShu");
		int shenQingSL=Integer.parseInt(request.getParameter("shenQingSL"));
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		if(type1==null && !currentUser.hasRole("部门负责人")){
			result="{\"result\":\"error\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		Date shenQingRQ=new Date();
		for(int i=0;i<shenQingSL;i++){
			ShenQing shenQing=new ShenQing();
			shenQing.setUser(user);
			shenQing.setDep(dep);
			shenQing.setShenQingRQ(shenQingRQ);
			shenQing.setShenQingYY(shenQingYY);
			shenQing.setWuZiMiaoShu(wuZiMiaoShu);
			shenQing.setShenQingSL(1);
			shenQing.setHuanJie(huanJieManager.get(102));
			
			shenQing.setDangQianShu(1);
			shenQing.setZongShu(0);
			shenQingManager.save(shenQing);
			if(type1==null && currentUser.hasRole("部门负责人")){
				shenQing.setHuanJie(huanJieManager.get(103));
				result="{\"result\":\"bumen\"}";
			}else{
				for(String userId:type1){
					User u0=userManager.getUser(userId);
					CaiGouSPR caiGouSPR=new CaiGouSPR();
					caiGouSPR.setShenQing(shenQing);
					caiGouSPR.setUser(u0);
					shenQing.setZongShu(shenQing.getZongShu()+1);
					caiGouSPRManager.save(caiGouSPR);
				}
			}
			shenQingManager.cover(shenQing);
			ShenQingJL shenQingJL=new ShenQingJL();
			shenQingJL.setShenQing(shenQing);
			shenQingJL.setPiFuLX("提交");
			shenQingJL.setPiFuNR("提交");
			shenQingJL.setCaoZuoRQ(shenQingRQ);
			shenQingJL.setHuanJie(huanJieManager.get(101));
			shenQingJL.setUser(user);
			shenQingJLManager.save(shenQingJL);
		}		
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 2.部门采购审批
	 */
	@RequestMapping(value = "/bmcgspList")
	public ModelAndView bmcgspList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("buMenCGSP", caiGouSPRManager.getMySPList(user));
		mav.setViewName("shenqing/bmcgspListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/buMenCaiGouTY")
	public void buMenCaiGouTY(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		CaiGouSPR caiGouSPR=caiGouSPRManager.get(id);
		ShenQing shenQing=caiGouSPR.getShenQing();
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setShenQing(shenQing);
		shenQingJL.setHuanJie(huanJieManager.get(102));
		caiGouSPRManager.delete(id);
		shenQingJL.setPiFuLX("通过");
		shenQingJL.setPiFuNR("同意");			
		shenQing.setDangQianShu(shenQing.getDangQianShu()+1);
		toDoManager.delete(shenQing,user);

		shenQing.setHuanJie(huanJieManager.get(103));
		shenQingManager.cover(shenQing);
		shenQingJL.setUser(user);
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJLManager.save(shenQingJL);
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/buMenCaiGouBTY")
	public ModelAndView buMenCaiGouBTY(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("caiGouSPRId", id);
		mav.setViewName("shenqing/buMenCGBTY");
		return mav;
	}
	
	@RequestMapping(value = "/buMenCGBTYSave")
	public void buMenCGSP(String id,String piFuNR,HttpServletResponse response) {
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		CaiGouSPR caiGouSPR=caiGouSPRManager.get(id);
		ShenQing shenQing=caiGouSPR.getShenQing();
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setShenQing(shenQing);
		shenQingJL.setHuanJie(huanJieManager.get(102));
		caiGouSPRManager.delete(id);
		shenQingJL.setPiFuLX("不通过");
		if(piFuNR!=null)
			shenQingJL.setPiFuNR(piFuNR);
		else
			shenQingJL.setPiFuNR("不同意");
		caiGouSPRManager.deleteAllSPR(shenQing.getId());
		shenQing.setHuanJie(huanJieManager.get(112));
		shenQingManager.cover(shenQing);
		toDoManager.deleteList(shenQing);
		shenQingJL.setUser(user);
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJLManager.save(shenQingJL);
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/buMenCGSPPLTY")
	public void buMenCGSPPLTY(HttpServletRequest request,HttpServletResponse response) {
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);		
		String[] type = request.getParameterValues("bmsqId");
		String result="{\"result\":\"success\"}";
		if(type==null){
			result="{\"result\":\"none\"}";
		}else{
			Email email=emailManager.getEmail();
			for (int i = 0; i < type.length; i++) {
				CaiGouSPR caiGouSPR=caiGouSPRManager.get(type[i]);
				ShenQing shenQing=caiGouSPR.getShenQing();			
				ShenQingJL shenQingJL=new ShenQingJL();
				shenQingJL.setHuanJie(huanJieManager.get(102));
				shenQingJL.setShenQing(shenQing);
				shenQingJL.setPiFuLX("通过");
				shenQingJL.setPiFuNR("同意");
				shenQingJL.setUser(user);
				shenQingJL.setCaoZuoRQ(new Date());
				shenQingJLManager.save(shenQingJL);
				caiGouSPRManager.delete(type[i]);
				shenQing.setDangQianShu(shenQing.getDangQianShu()+1);
				
				toDoManager.delete(shenQing,user);
				if(shenQing.getDangQianShu()-1==shenQing.getZongShu()){
					shenQing.setHuanJie(huanJieManager.get(103));
					String root=rootManager.getRoot();
					//采购部门负责人选择采买人
					for(User user1:userManager.getCGBMFZR()){
						ToDo toDo=new ToDo();
						toDo.setUser(user1);
						toDo.setHuanJie(huanJieManager.get(103));
						toDo.setCreateDate(new Date());
						toDo.setTask("选择采买人");
						toDo.setType("申请采购");
						toDo.setShenQing(shenQing);
						toDoManager.save(toDo);
						//\\发送邮件
						YouJian youJian=youJianManager.get(103);
						if(youJian!=null && youJian.getState()==1){
							SendMailThread st=new SendMailThread(email, root, 103, user1, youJian);
							st.setShenQing(shenQing);
							Thread t=new Thread(st);
							t.start();
						}
					}
				}
				shenQingManager.cover(shenQing);
			}
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/buMenCGSPPLBTY")
	public void buMenCGSPPLBTY(HttpServletRequest request,HttpServletResponse response) {		
		String[] type = request.getParameterValues("bmsqId");
		String result="{\"result\":\"none\"}";
		if(type!=null){
			result="{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/buMenCGSPPLBTYPage")
	public ModelAndView buMenCGSPPLBTYPage(HttpServletRequest request) {		
		ModelAndView mav=new ModelAndView();
		String[] type = request.getParameterValues("bmsqId");
		String bmsqIds="";
		if(type!=null){
			for(int i=0;i<type.length;i++){
				bmsqIds=bmsqIds+type[i]+",";
			}
		}
		if(bmsqIds.length()>0)
			bmsqIds=bmsqIds.substring(0,bmsqIds.length()-1);
		mav.addObject("bmsqIds", bmsqIds);
		mav.setViewName("shenqing/buMenCGSPPLBTY");
		return mav;
	}
	
	@RequestMapping(value = "/buMenCGSPPLBTYSave")
	public void buMenCGSPPLBTYSave(String bmsqIds,String piFuNR,HttpServletResponse response) {
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String result="{\"result\":\"error\"}";
		String bmsqId[]=bmsqIds.split(",");
		for(int i=0;i<bmsqId.length;i++){
			CaiGouSPR caiGouSPR=caiGouSPRManager.get(bmsqId[i]);
			ShenQing shenQing=caiGouSPR.getShenQing();			
			ShenQingJL shenQingJL=new ShenQingJL();
			shenQingJL.setHuanJie(huanJieManager.get(102));
			shenQingJL.setShenQing(shenQing);
			shenQingJL.setPiFuLX("不通过");
			if(piFuNR!=null)
				shenQingJL.setPiFuNR(piFuNR);
			else
				shenQingJL.setPiFuNR("不同意");
			shenQingJL.setUser(user);
			shenQingJL.setCaoZuoRQ(new Date());
			shenQingJLManager.save(shenQingJL);
			caiGouSPRManager.deleteAllSPR(shenQing.getId());
			shenQing.setDangQianShu(shenQing.getDangQianShu()+1);
			shenQing.setHuanJie(huanJieManager.get(112));
			shenQingManager.cover(shenQing);
			toDoManager.deleteList(shenQing);
			result="{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 3.采购部门负责人审批
	 */
	@RequestMapping(value = "/xzcmrList")
	public ModelAndView xzcmrList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("caiGouXZCMR", shenQingManager.getAllCaiGouZT(103));
		mav.setViewName("shenqing/xzcmrListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/xuanZeCMR")
	public ModelAndView xuanZeCMR(String id){
		ModelAndView mav=new ModelAndView();		
		mav.addObject("shenQing", shenQingManager.get(id));
		mav.addObject("depList", depManager.getAllDep());
		mav.setViewName("shenqing/cMRZD");
		return mav;
	}
	
	@RequestMapping(value = "/xuanZeCMRSave")
	public void xuanZeCMRSave(String id,String caiMaiRId,HttpServletResponse response) {
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		ShenQing shenQing=shenQingManager.get(id);		
		shenQing.setHuanJie(huanJieManager.get(104));
		shenQing.setCaiMaiR(userManager.getUser(caiMaiRId));
		shenQingManager.cover(shenQing);
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setHuanJie(huanJieManager.get(103));
		shenQingJL.setShenQing(shenQing);
		shenQingJL.setPiFuLX("选择采买人");
		shenQingJL.setPiFuNR("选择采买人");
		shenQingJL.setUser(user);
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJLManager.save(shenQingJL);
		//采购部门负责人选择采买人
		toDoManager.deleteList(shenQing);
		ToDo toDo=new ToDo();
		toDo.setCreateDate(new Date());
		toDo.setHuanJie(huanJieManager.get(104));
		toDo.setShenQing(shenQing);
		toDo.setTask("采购审批");
		toDo.setType("申请采购");
		toDo.setUser(userManager.getUser(caiMaiRId));
		toDoManager.save(toDo);
		//\\发送邮件
		Email email=emailManager.getEmail();
		YouJian youJian=youJianManager.get(104);
		String root=rootManager.getRoot();
		if(youJian.getState()==1){
			SendMailThread st=new SendMailThread(email, root, 104, userManager.getUser(caiMaiRId), youJian);
			st.setShenQing(shenQing);
			Thread t=new Thread(st);
			t.start();
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/xzcmrPL")
	public void xzcmrPL(HttpServletRequest request,HttpServletResponse response) {		
		String[] type = request.getParameterValues("cmrId");
		String result="{\"result\":\"none\"}";
		if(type!=null){
			result="{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/cmrPLZD")
	public ModelAndView cmrPLZD(HttpServletRequest request) {		
		ModelAndView mav=new ModelAndView();
		String type[]=request.getParameterValues("cmrId");
		String cmrIds="";
		if(type!=null){
			for(int i=0;i<type.length;i++){
				cmrIds=cmrIds+type[i]+",";
			}
		}
		if(cmrIds.length()>0)
			cmrIds=cmrIds.substring(0, cmrIds.length()-1);
		mav.addObject("cmrIds", cmrIds);
		mav.addObject("depList", depManager.getAllDep());
		mav.setViewName("shenqing/cmrPLZD");
		return mav;
	}
	
	@RequestMapping(value = "/xuanZeCMRPLSave")
	public void xuanZeCMRPLSave(String cmrIds,String caiMaiRId,HttpServletResponse response) {
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String cmrId[]=cmrIds.split(",");
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		for(int i=0;i<cmrId.length;i++){
			ShenQing shenQing=shenQingManager.get(cmrId[i]);		
			shenQing.setHuanJie(huanJieManager.get(104));
			shenQing.setCaiMaiR(userManager.getUser(caiMaiRId));
			shenQingManager.cover(shenQing);
			ShenQingJL shenQingJL=new ShenQingJL();
			shenQingJL.setHuanJie(huanJieManager.get(103));
			shenQingJL.setShenQing(shenQing);
			shenQingJL.setPiFuLX("选择采买人");
			shenQingJL.setPiFuNR("选择采买人");
			shenQingJL.setUser(user);
			shenQingJL.setCaoZuoRQ(new Date());
			shenQingJLManager.save(shenQingJL);
			//低值管理员选择采买人
			toDoManager.deleteList(shenQing);
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(104));
			toDo.setShenQing(shenQing);
			toDo.setTask("采购审批");
			toDo.setType("申请采购");
			toDo.setUser(userManager.getUser(caiMaiRId));
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(104);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 104, userManager.getUser(caiMaiRId), youJian);
				st.setShenQing(shenQing);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/xzcmrBTY")
	public ModelAndView xzcmrBTY(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("shenQing", shenQingManager.get(id));
		mav.setViewName("shenqing/xzcmrBTY");
		return mav;
	}
	
	@RequestMapping(value = "/xzcmrBTYSave")
	public void xzcmrBTYSave(String sqId,String piFuNR,HttpServletResponse response) {
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		ShenQing shenQing=shenQingManager.get(sqId);		
		shenQing.setHuanJie(huanJieManager.get(112));
		shenQingManager.cover(shenQing);
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setHuanJie(huanJieManager.get(103));
		shenQingJL.setShenQing(shenQing);
		shenQingJL.setPiFuLX("选择采买人");
		shenQingJL.setPiFuNR(piFuNR);
		shenQingJL.setUser(user);
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJLManager.save(shenQingJL);
		//低值管理员选择采买人
		toDoManager.deleteList(shenQing);
		
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/xzcmrPLBTY")
	public ModelAndView xzcmrPLBTY(HttpServletRequest request) {		
		ModelAndView mav=new ModelAndView();
		String type[]=request.getParameterValues("cmrId");
		String cmrIds="";
		if(type!=null){
			for(int i=0;i<type.length;i++){
				cmrIds=cmrIds+type[i]+",";
			}
		}
		if(cmrIds.length()>0)
			cmrIds=cmrIds.substring(0, cmrIds.length()-1);
		mav.addObject("sqIds", cmrIds);
		mav.setViewName("shenqing/xzcmrPLBTY");
		return mav;
	}
	
	@RequestMapping(value = "/xzcmrPLBTYSave")
	public void xzcmrPLBTYSave(String sqIds,String piFuNR,HttpServletResponse response) {
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String sqId[]=sqIds.split(",");
		for(int i=0;i<sqId.length;i++){
			ShenQing shenQing=shenQingManager.get(sqId[i]);		
			shenQing.setHuanJie(huanJieManager.get(112));
			shenQingManager.cover(shenQing);
			ShenQingJL shenQingJL=new ShenQingJL();
			shenQingJL.setHuanJie(huanJieManager.get(103));
			shenQingJL.setShenQing(shenQing);
			shenQingJL.setPiFuLX("选择采买人");
			shenQingJL.setPiFuNR(piFuNR);
			shenQingJL.setUser(user);
			shenQingJL.setCaoZuoRQ(new Date());
			shenQingJLManager.save(shenQingJL);
			//低值管理员选择采买人
			toDoManager.deleteList(shenQing);
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 4.采买人采购审批
	 */
	@RequestMapping(value = "/cgspList")
	public ModelAndView cgspList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("caiGouSP", shenQingManager.getAllCaiGouZT2CMR(104,user));
		mav.setViewName("shenqing/cgspListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/getBianMa")
	public void getBianMa(String wuLiaoBM,HttpServletResponse response){
		BianMa bianMa=bianMaManager.getBianMa(wuLiaoBM);
		response.setContentType("application/json;charset=utf-8");
		String result="{\"zhuangTai\":\"2\"}";
		if(bianMa!=null){
			String wuLiaoMC=bianMa.getWuLiaoMC();
			String guiGeXH=bianMa.getGuiGeXH();
			result="{\"zhuangTai\":\"1\",\"wuLiaoMC\":\""+wuLiaoMC+"\",\"guiGeXH\":\""+guiGeXH+"\"}";
		}
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/caiMaiEdit")
	public ModelAndView caiMaiEdit(String id) {		
		ModelAndView mav = new ModelAndView();		
		mav.addObject("wuZiLB1", wuZiLBManager.getAllWuZiLB1());		
		mav.addObject("biZhong", biZhongManager.getAllBiZhong());
		mav.addObject("shenQing", shenQingManager.get(id));
		String format="yyyy-MM-dd";
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		String dangQianRQ=sdf.format(new Date());
		mav.addObject("dangQianRQ", dangQianRQ);
		mav.setViewName("shenqing/wuZiCGEdit");
		return mav;
	}
	
	@RequestMapping(value = "/upHeTongForm")
	public ModelAndView upHeTongForm(String shenQingId,String type){
		ModelAndView mav=new ModelAndView();
		mav.addObject("shenQingId", shenQingId);
		mav.addObject("type", type);
		mav.setViewName("shenqing/upFuJianForm");
		return mav;
	}
	
	@RequestMapping(value = "/xzHeTongForm")
	public ModelAndView xzHeTongForm(String type){
		ModelAndView mav=new ModelAndView();
		mav.addObject("type", type);
		mav.addObject("fileList", heTongManager.list());
		mav.setViewName("shenqing/xzFuJianForm");
		return mav;
	}
	
	@RequestMapping(value = "/upFuJianForm")
	public ModelAndView upFujianForm(String shenQingId,String type){
		ModelAndView mav=new ModelAndView();
		mav.addObject("shenQingId", shenQingId);
		mav.addObject("type", type);
		mav.setViewName("shenqing/upFuJianForm");
		return mav;
	}
	
	@RequestMapping(value = "/xzFuJianForm")
	public ModelAndView xzFuJianForm(String type){
		ModelAndView mav=new ModelAndView();
		mav.addObject("type", type);
		mav.addObject("fileList", zhengMingManager.list());
		mav.setViewName("shenqing/xzFuJianForm");
		return mav;
	}
	
	@RequestMapping(value = "/cgWuZiSave",method=RequestMethod.POST)
	public void cgWuZiSave(String shenQingId,WuZi wuZi,String biZhongId,String wuZiLBId,String gouMaiRQStr,HttpServletResponse response) {
		response.setContentType("application/json");
		ShenQing wzsq=shenQingManager.get(shenQingId);
		wzsq.setHuanJie(huanJieManager.get(105));
		shenQingManager.cover(wzsq);
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setUser(user);
		shenQingJL.setPiFuLX("通过");
		shenQingJL.setPiFuNR("同意并采购");
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJL.setShenQing(wzsq);
		shenQingJL.setHuanJie(huanJieManager.get(104));
		shenQingJLManager.save(shenQingJL);
		WuZi wz=wuZi;
		String format="yyyy-MM-dd";
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		try{
		    wz.setGouMaiRQ(sdf.parse(gouMaiRQStr));
		}catch(Exception e){
			e.printStackTrace();
		}
		wz.setCaiMaiR(user);
		wz.setWuZiLB(wuZiLBManager.get(wuZiLBId));
		wz.setBiZhong(biZhongManager.get(biZhongId));
		wz.setShiYongZT(shiYongZTManager.get(101));
		wuZiManager.save(wz);
		wzsq.setWuZi(wz);
		shenQingManager.cover(wzsq);
		//验收入库
		toDoManager.deleteList(wzsq);
		
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(105));
			toDo.setShenQing(wzsq);
			toDo.setTask("验收入库");
			toDo.setType("申请采购");
			toDo.setUser(user1);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(105);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 105, user1, youJian);
				st.setShenQing(wzsq);
				st.setWuZi(wz);
				Thread t=new Thread(st);
				t.start();
			}
		}
		try {
			String result="{\"wuZiId\":\""+wz.getId()+"\"}";
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/caiGouSave")
	public void caiGouSave(String wuZiId,CaiGou cg,String heTong,String fuJian,HttpServletResponse response){
		WuZi wuZi=wuZiManager.get(wuZiId);
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		CaiGou caiGou=new CaiGou();
		caiGou.setBeiZhu(cg.getBeiZhu());
		caiGou.setCaiGouRQ(new Date());
		caiGou.setTuJing(cg.getTuJing());
		caiGou.setFangShi(cg.getFangShi());
		caiGou.setFaPiaoHao(cg.getFaPiaoHao());
		caiGou.setUser(user);
		caiGou.setWuZi(wuZi);
		caiGouManager.save(caiGou);
		if(heTong!=null && heTong.length()>0){
			heTong=heTong.substring(0, heTong.length()-1);
			String hId[]=heTong.split(",");
			for(int i=0;i<hId.length;i++){
				HeTong ht=heTongManager.get(hId[i]);
				if(ht.getCaiGou()==null){
					ht.setCaiGou(caiGou);
					heTongManager.cover(ht);
				}else{
					HeTong he=new HeTong();
					he.setCaiGou(caiGou);
					he.setWenJianM(ht.getWenJianM());
					he.setXiangDuiLJ(ht.getXiangDuiLJ());
					he.setYsWJM(ht.getYsWJM());
					heTongManager.save(he);
				}
			}
		}
		if(fuJian!=null && fuJian.length()>0){
			fuJian=fuJian.substring(0, fuJian.length()-1);
			String fId[]=fuJian.split(",");
			for(int i=0;i<fId.length;i++){
				ZhengMing zhengMing=zhengMingManager.get(fId[i]);
				if(zhengMing.getCaiGou()==null){
					zhengMing.setCaiGou(caiGou);
					zhengMingManager.cover(zhengMing);
				}else{
					ZhengMing zm=new ZhengMing();
					zm.setCaiGou(caiGou);
					zm.setWenJianM(zhengMing.getWenJianM());
					zm.setXiangDuiLJ(zhengMing.getXiangDuiLJ());
					zm.setYsWJM(zhengMing.getYsWJM());
					zhengMingManager.save(zhengMing);
				}
			}
		}
		response.setContentType("application/json");
		try {
			String result="{\"result\":\"success\"}";
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/cgspPL")
	public void cgspPL(HttpServletRequest request,HttpServletResponse response) {		
		String[] type = request.getParameterValues("sqId");
		String result="{\"result\":\"none\"}";
		if(type!=null){
			result="{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/cgspPLTY")
	public ModelAndView cgspPLTY(HttpServletRequest request) {
		ModelAndView mav=new ModelAndView();
		String[] type = request.getParameterValues("sqId");
		if(type!=null){
			String sqIds="";
			for(int i=0;i<type.length;i++){
				sqIds=sqIds+type[i]+",";
			}
			if(sqIds.length()>0)
				sqIds=sqIds.substring(0, sqIds.length()-1);
			mav.addObject("sqIds", sqIds);
		}
		mav.addObject("wuZiLB1", wuZiLBManager.getAllWuZiLB1());		
		mav.addObject("biZhong", biZhongManager.getAllBiZhong());
		String format="yyyy-MM-dd";
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		String dangQianRQ=sdf.format(new Date());
		mav.addObject("dangQianRQ", dangQianRQ);
		mav.setViewName("shenqing/cgspPLTY");
		return mav;
	}
	
	@RequestMapping(value = "/cgWuZiPLSave",method=RequestMethod.POST)
	public void cgWuZiPLSave(String sqIds,WuZi wuZi,String biZhongId,String wuZiLBId,String gouMaiRQStr,HttpServletResponse response) {
		response.setContentType("application/json");
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String wuZiIds="";
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		if(sqIds!=null){
			String sqId[]=sqIds.split(",");
			for(int i=0;i<sqId.length;i++){
				ShenQing wzsq=shenQingManager.get(sqId[i]);
				wzsq.setHuanJie(huanJieManager.get(105));
				shenQingManager.cover(wzsq);
				ShenQingJL shenQingJL=new ShenQingJL();
				shenQingJL.setUser(user);
				shenQingJL.setPiFuLX("通过");
				shenQingJL.setPiFuNR("同意并采购");
				shenQingJL.setCaoZuoRQ(new Date());
				shenQingJL.setShenQing(wzsq);
				shenQingJL.setHuanJie(huanJieManager.get(104));
				shenQingJLManager.save(shenQingJL);
				WuZi wz=new WuZi();
				wz.setWuLiaoBM(wz.getWuLiaoBM());
				wz.setWuZiMC(wuZi.getWuZiMC());
				wz.setPinPai(wuZi.getPinPai());
				wz.setGuiGeXH(wuZi.getGuiGeXH());
				wz.setShengChanCJ(wuZi.getShengChanCJ());
				wz.setWuZiYZ(wuZi.getWuZiYZ());
				wz.setGongYingShangMC(wuZi.getGongYingShangMC());
				wz.setWuZiLX(wuZi.getWuZiLX());
				String format="yyyy-MM-dd";
				SimpleDateFormat sdf=new SimpleDateFormat(format);
				try{
				    wz.setGouMaiRQ(sdf.parse(gouMaiRQStr));
				}catch(Exception e){
					e.printStackTrace();
				}
				wz.setCaiMaiR(user);
				wz.setWuZiLB(wuZiLBManager.get(wuZiLBId));
				wz.setBiZhong(biZhongManager.get(biZhongId));
				wz.setShiYongZT(shiYongZTManager.get(101));
				wuZiManager.save(wz);
				wuZiIds=wuZiIds+wz.getId()+",";
				wzsq.setWuZi(wz);
				shenQingManager.cover(wzsq);
				//验收入库
				toDoManager.deleteList(wzsq);
				for(User user1:userManager.getGZGLY()){
					ToDo toDo=new ToDo();
					toDo.setCreateDate(new Date());
					toDo.setHuanJie(huanJieManager.get(105));
					toDo.setShenQing(wzsq);
					toDo.setTask("验收入库");
					toDo.setType("申请采购");
					toDo.setUser(user1);
					toDoManager.save(toDo);
					//\\发送邮件
					YouJian youJian=youJianManager.get(105);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 105, user1, youJian);
						st.setShenQing(wzsq);
						st.setWuZi(wz);
						Thread t=new Thread(st);
						t.start();
					}
				}
			}
		}
		try {
			if(wuZiIds.length()>0){
				wuZiIds=wuZiIds.substring(0, wuZiIds.length()-1);
			}
			String result="{\"wuZiIds\":\""+wuZiIds+"\"}";
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/caiGouPLSave")
	public void caiGouPLSave(String wuZiIds,CaiGou cg,String heTong,String fuJian,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		if(heTong!=null && heTong.length()>0)
			heTong=heTong.substring(0, heTong.length()-1);
		if(fuJian!=null && fuJian.length()>0)
			fuJian=fuJian.substring(0, fuJian.length()-1);
		if(wuZiIds!=null){
			String wuZiId[]=wuZiIds.split(",");
			for(int m=0;m<wuZiId.length;m++){
				WuZi wuZi=wuZiManager.get(wuZiId[m]);
				CaiGou caiGou=new CaiGou();
				caiGou.setBeiZhu(cg.getBeiZhu());
				caiGou.setCaiGouRQ(new Date());
				caiGou.setTuJing(cg.getTuJing());
				caiGou.setFangShi(cg.getFangShi());
				caiGou.setUser(user);
				caiGou.setWuZi(wuZi);
				caiGouManager.save(caiGou);
				if(heTong!=null && heTong.length()>0){
					String hId[]=heTong.split(",");
					for(int i=0;i<hId.length;i++){
						HeTong ht=heTongManager.get(hId[i]);
						if(ht.getCaiGou()==null){
							ht.setCaiGou(caiGou);
							heTongManager.cover(ht);
						}else{
							HeTong he=new HeTong();
							he.setCaiGou(caiGou);
							he.setWenJianM(ht.getWenJianM());
							he.setXiangDuiLJ(ht.getXiangDuiLJ());
							he.setYsWJM(ht.getYsWJM());
							heTongManager.save(he);
						}
					}
				}
				if(fuJian!=null && fuJian.length()>0){
					String fId[]=fuJian.split(",");
					for(int i=0;i<fId.length;i++){
						ZhengMing zhengMing=zhengMingManager.get(fId[i]);
						if(zhengMing.getCaiGou()==null){
							zhengMing.setCaiGou(caiGou);
							zhengMingManager.cover(zhengMing);
						}else{
							ZhengMing zm=new ZhengMing();
							zm.setCaiGou(caiGou);
							zm.setWenJianM(zhengMing.getWenJianM());
							zm.setXiangDuiLJ(zhengMing.getXiangDuiLJ());
							zm.setYsWJM(zhengMing.getYsWJM());
							zhengMingManager.save(zm);
						}
					}
				}
			}
		}
		response.setContentType("application/json");
		try {
			String result="{\"result\":\"success\"}";
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/caiGouBTY")
	public ModelAndView caiGouBTY(String id){
		ModelAndView mav=new ModelAndView();		
		mav.addObject("shenQingId", id);
		mav.setViewName("shenqing/caiGouBTY");
		return mav;
	}
	
	@RequestMapping(value = "/caiGouBTYSave")
	public void caiGouBTYSave(String id,String piFuNR,HttpServletResponse response) {
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);		
		ShenQing shenQing=shenQingManager.get(id);
		shenQing.setHuanJie(huanJieManager.get(103));
		shenQingManager.cover(shenQing);
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setHuanJie(huanJieManager.get(104));
		shenQingJL.setShenQing(shenQing);
		shenQingJL.setPiFuLX("申请驳回");
		shenQingJL.setPiFuNR(piFuNR);
		shenQingJL.setUser(user);
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJLManager.save(shenQingJL);
		//申请采购驳回
		toDoManager.deleteList(shenQing);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		//采购部门负责任人选择采买人
		for(User user1:userManager.getCGBMFZR()){
			ToDo toDo=new ToDo();
			toDo.setUser(user1);
			toDo.setHuanJie(huanJieManager.get(103));
			toDo.setCreateDate(new Date());
			toDo.setTask("选择采买人");
			toDo.setType("申请采购");
			toDo.setShenQing(shenQing);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(103);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 103, user1, youJian);
				st.setShenQing(shenQing);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/cgspPLBTY")
	public ModelAndView cgspPLBTY(HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		String[] type = request.getParameterValues("sqId");
		if(type!=null){
			String sqIds="";
			for(int i=0;i<type.length;i++){
				sqIds=sqIds+type[i]+",";
			}
			if(sqIds.length()>0)
				sqIds=sqIds.substring(0, sqIds.length()-1);
			mav.addObject("sqIds", sqIds);
		}
		mav.setViewName("shenqing/caiGouPLBTY");
		return mav;
	}
	
	@RequestMapping(value = "/caiGouPLBTYSave")
	public void caiGouPLBTYSave(String sqIds,String piFuNR,HttpServletResponse response) {
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		String sqId[]=sqIds.split(",");
		for(int i=0;i<sqId.length;i++){
			ShenQing shenQing=shenQingManager.get(sqId[i]);
			shenQing.setHuanJie(huanJieManager.get(103));
			shenQingManager.cover(shenQing);
			ShenQingJL shenQingJL=new ShenQingJL();
			shenQingJL.setHuanJie(huanJieManager.get(104));
			shenQingJL.setShenQing(shenQing);
			shenQingJL.setPiFuLX("申请驳回");
			shenQingJL.setPiFuNR(piFuNR);
			shenQingJL.setUser(user);
			shenQingJL.setCaoZuoRQ(new Date());
			shenQingJLManager.save(shenQingJL);
			//申请采购驳回
			toDoManager.deleteList(shenQing);
			//采购部门负责任人选择采买人
			for(User user1:userManager.getCGBMFZR()){
				ToDo toDo=new ToDo();
				toDo.setUser(user1);
				toDo.setHuanJie(huanJieManager.get(103));
				toDo.setCreateDate(new Date());
				toDo.setTask("选择采买人");
				toDo.setType("申请采购");
				toDo.setShenQing(shenQing);
				toDoManager.save(toDo);
				//\\发送邮件
				YouJian youJian=youJianManager.get(103);
				if(youJian!=null && youJian.getState()==1){
					SendMailThread st=new SendMailThread(email, root, 103, user1, youJian);
					st.setShenQing(shenQing);
					Thread t=new Thread(st);
					t.start();
				}
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 5. 低值管理员采购入库
	 */
	@RequestMapping(value = "/ysrkList")
	public ModelAndView ysrkList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("yanShouRK", shenQingManager.getAllYanShouRK());
		mav.setViewName("shenqing/ysrkListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/getZiChanBH")
	public void getZiChanBH(String id,HttpServletResponse response){
		WuZi wuZi=wuZiManager.get(id);
		if(wuZi.getZiChanBH()==null){
			String format="yyyyMMdd";
			SimpleDateFormat sdf=new SimpleDateFormat(format);
			String rq="D" + sdf.format(wuZi.getGouMaiRQ());
			rq+=wuZi.getWuZiLB().getLeiBieJC();
			int bianHao=wuZiManager.getBianHao(rq);
			rq+=String.format("%04d", bianHao);
			wuZi.setZiChanBH(rq);
			wuZiManager.cover(wuZi);
		}
		response.setContentType("application/json");
		String result="{\"result\":\""+wuZi.getZiChanBH()+"\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/caiGouRK")
	public ModelAndView caiGouRK(String id){
		ModelAndView mav=new ModelAndView();
		ShenQing shenQing=shenQingManager.get(id);
		if(shenQing!=null){
			WuZi wuZi=shenQing.getWuZi();
			String format="yyyyMMdd";
			SimpleDateFormat sdf=new SimpleDateFormat(format);
			String rq=sdf.format(wuZi.getGouMaiRQ());
			rq+=wuZi.getWuZiLB().getLeiBieJC();
			int bianHao=wuZiManager.getBianHao(rq);
			rq+=String.format("%04d", bianHao);
			mav.addObject("ziChanBHZD", rq);
			mav.addObject("shenQing", shenQing);
			mav.addObject("wuZiLB1", wuZiLBManager.getAllWuZiLB1());
			mav.addObject("biZhong", biZhongManager.getAllBiZhong());
			mav.addObject("depList", depManager.getAllDep());
			mav.setViewName("shenqing/caiGouRK");
		}
		return mav;
	}
	
	@RequestMapping(value = "/caiGouRKBaoCun")
	public void caiGouRKBaoCun(String shenQingId,WuZi temp,String baoXiuJZRQStr,String guanLiDepId,String zheJiuLX,HttpServletResponse response){
		WuZi wuZi=wuZiManager.get(temp.getId());
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		wuZi.setWuZiMC(temp.getWuZiMC());
		wuZi.setPinPai(temp.getPinPai());
		wuZi.setShengChanCJ(temp.getShengChanCJ());
		wuZi.setGuiGeXH(temp.getGuiGeXH());
		wuZi.setRuKuRQ(new Date());
		wuZi.setGongYingShangMC(temp.getGongYingShangMC());
		wuZi.setGuanLiBM(depManager.get(guanLiDepId));
		wuZi.setBeiZhu(temp.getBeiZhu());
		wuZi.setWuZiYZ(temp.getWuZiYZ());
		wuZi.setWuZiJingZhi(temp.getWuZiYZ());
		wuZi.setZiChanBM("");
		wuZi.setChanPinXLH(temp.getChanPinXLH());
		wuZi.setJingCanZhiLv(temp.getJingCanZhiLv());
		wuZi.setZengJiaFS(temp.getZengJiaFS());
		wuZi.setZheJiuYX(temp.getZheJiuYX());
		wuZi.setBaoFeiYX(temp.getBaoFeiYX());
		wuZi.setZheJiuYS(temp.getZheJiuYS());
		wuZi.setCunFangD("");
		try{
			String format2="yyyy-MM-dd";
			SimpleDateFormat sdf2=new SimpleDateFormat(format2);
		    wuZi.setBaoXiuJZRQ(sdf2.parse(baoXiuJZRQStr));
		}catch(Exception e){			
		}
		wuZi.setShiYongZT(shiYongZTManager.get(105));
		//wuZi.setZheJiuFS(zheJiuFSManager.get(zheJiuLX));
		wuZiManager.cover(wuZi);
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/caiGouRKSave")
	public void caiGouRKSave(String shenQingId,WuZi temp,String baoXiuJZRQStr,String guanLiDepId,String zheJiuLX,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		WuZi wuZi=wuZiManager.get(temp.getId());
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		String format="yyyyMMdd";
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		if(wuZi.getZiChanBH()==null){
			String rq="D" + sdf.format(wuZi.getGouMaiRQ());
			rq+=wuZi.getWuZiLB().getLeiBieJC();
			int bianHao=wuZiManager.getBianHao(rq);
			rq+=String.format("%04d", bianHao);
			wuZi.setZiChanBH(rq);
		}
		wuZi.setWuZiMC(temp.getWuZiMC());
		wuZi.setPinPai(temp.getPinPai());
		wuZi.setShengChanCJ(temp.getShengChanCJ());
		wuZi.setGuiGeXH(temp.getGuiGeXH());
		wuZi.setRuKuRQ(new Date());
		wuZi.setGongYingShangMC(temp.getGongYingShangMC());
		wuZi.setGuanLiBM(depManager.get(guanLiDepId));
		wuZi.setBeiZhu(temp.getBeiZhu());
		wuZi.setWuZiYZ(temp.getWuZiYZ());
		wuZi.setWuZiJingZhi(temp.getWuZiYZ());
		wuZi.setZiChanBM("");
		wuZi.setChanPinXLH(temp.getChanPinXLH());
		wuZi.setJingCanZhiLv(temp.getJingCanZhiLv());
		wuZi.setZengJiaFS(temp.getZengJiaFS());
		wuZi.setZheJiuYX(temp.getZheJiuYX());
		wuZi.setBaoFeiYX(temp.getBaoFeiYX());
		wuZi.setZheJiuYS(temp.getZheJiuYS());
		wuZi.setCunFangD("");
		wuZi.setPanDian(0);
		try{
			String format2="yyyy-MM-dd";
			SimpleDateFormat sdf2=new SimpleDateFormat(format2);
		    wuZi.setBaoXiuJZRQ(sdf2.parse(baoXiuJZRQStr));
		}catch(Exception e){			
		}
		wuZi.setShiYongZT(shiYongZTManager.get(105));
		//wuZi.setZheJiuFS(zheJiuFSManager.get(zheJiuLX));
		wuZiManager.cover(wuZi);
		ShenQing shenQing=shenQingManager.get(shenQingId);
		if(shenQing.getHuanJie().getId()==203){
			HuanHuo huanHuo=huanHuoManager.getByShenQing(shenQing);
			huanHuo.setXinChanPinXLH(temp.getChanPinXLH());
			huanHuoManager.cover(huanHuo);
			HuanHuoJL huanHuoJL=new HuanHuoJL();
			huanHuoJL.setCaoZuoRQ(new Date());
			huanHuoJL.setHuanHuo(huanHuo);
			huanHuoJL.setHuanJie(huanJieManager.get(203));
			huanHuoJL.setPiFuLX("同意入库");
			huanHuoJL.setPiFuNR("同意");
			huanHuoJL.setUser(user);
			huanHuoJLManager.save(huanHuoJL);
		}
		shenQing.setHuanJie(huanJieManager.get(106));		
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setUser(user);
		shenQingJL.setPiFuLX("通过");
		shenQingJL.setPiFuNR("验收入库");
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJL.setShenQing(shenQing);
		shenQingJL.setHuanJie(huanJieManager.get(105));
		shenQingJLManager.save(shenQingJL);
		
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		//物资发放
		toDoManager.deleteList(shenQing);
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(106));
			toDo.setShenQing(shenQing);
			toDo.setTask("验收入库");
			toDo.setType("申请采购");
			toDo.setUser(user1);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(106);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 106, user1, youJian);
				st.setWuZi(wuZi);
				Thread t=new Thread(st);
				t.start();
			}
		}
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 低值管理员 物资发放
	 */
	@RequestMapping(value = "/cgffList")
	public ModelAndView cgffList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("caiGouFF", shenQingManager.getAllCaiGouZT(106));
		mav.setViewName("shenqing/cgffListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/faFang")
	public ModelAndView faFang(String id){
		ModelAndView mav=new ModelAndView();
		WuZi wuZi=wuZiManager.get(id);
		mav.addObject("wuZi", wuZi);
		mav.addObject("shiYongdepId", wuZi.getShenQing().iterator().next().getDep().getId());
		mav.addObject("shiYonguserId", wuZi.getShenQing().iterator().next().getUser().getId());
		mav.addObject("depList", depManager.getAllDep());
		mav.setViewName("shenqing/faFang");
		return mav;
	}
	
	@RequestMapping(value = "/getWuZiFenTanBM")
	public ModelAndView getWuZiFenTanBM(String id){		
		WuZi wuZi=wuZiManager.get(id);
		ModelAndView mav=new ModelAndView();
		mav.addObject("fenTanBMList", fenTanManager.getFenTanBM(wuZi));
		mav.setViewName("wuzi/wuZiFTBM");
		return mav;
	}
	
	@RequestMapping(value = "/addFenTanBM")
	public ModelAndView addFenTanBM(String wuZiId,String depId,Float fenTanBL,HttpServletResponse response){		
		WuZi wuZi=wuZiManager.get(wuZiId);
		FenTan fenTan=new FenTan();
		fenTan.setDep(depManager.get(depId));
		fenTan.setFenTanZT(1);
		if(fenTanManager.chongFu(wuZiId,depId)){
			response.setContentType("application/json");
			String result="{\"result\":\"chongfu\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
		List<FenTan> fenTanList=fenTanManager.getFenTanBM(wuZi);
		float bili=0f;
		for(FenTan t:fenTanList){
			bili+=t.getPercent();
		}
		bili+=fenTanBL;
		if(bili>100){
			response.setContentType("application/json");
			String result="{\"result\":\"dayu\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
		
		fenTan.setZengJiaRQ(new Date());
		fenTan.setWuZi(wuZi);
		fenTan.setPercent(fenTanBL);
		fenTanManager.save(fenTan);
		
		ModelAndView mav=new ModelAndView();
		mav.addObject("fenTanBMList", fenTanManager.getFenTanBM(wuZi));
		mav.setViewName("wuzi/wuZiFTBM");
		return mav;
	}
	
	@RequestMapping(value = "/shanChuFTBM")
	public ModelAndView shanChuFTBM(String id,String wuZiId){
		WuZi wuZi=wuZiManager.get(wuZiId);
		FenTan fenTan=fenTanManager.get(id);
		fenTan.setJianShaoRQ(new Date());
		fenTan.setFenTanZT(2);
		fenTanManager.cover(fenTan);
		ModelAndView mav=new ModelAndView();
		mav.addObject("fenTanBMList", fenTanManager.getFenTanBM(wuZi));
		mav.setViewName("wuzi/wuZiFTBM");
		return mav;
	}
	
	@RequestMapping(value = "/caiGouFaFangSave")
	public void caiGouFaFangSave(String wuZiId,String cunFangD,String beiZhu,String shiYongdepId,String shiYonguserId,HttpServletResponse response){
		WuZi wuZi=wuZiManager.get(wuZiId);
		List<FenTan> fenTanList=fenTanManager.getFenTanBM(wuZi);
		float bili=0f;
		for(FenTan t:fenTanList){
			bili+=t.getPercent();			
		}
		if(bili>0 && bili<100){
			response.setContentType("application/json");
			String result="{\"result\":\"bili\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		else if(bili==0){
			FenTan fenTan=new FenTan();
			fenTan.setDep(depManager.get(shiYongdepId));
			fenTan.setZengJiaRQ(new Date());
			fenTan.setWuZi(wuZi);
			fenTan.setFenTanZT(1);
			fenTan.setPercent(100f);
			fenTanManager.save(fenTan);
		}
		wuZi.setCunFangD(cunFangD);		
		wuZi.setShiYongZT(shiYongZTManager.get(102));
		ShenQing shenQing=shenQingManager.getShenQingForWuZi(wuZi);
		// 取消此环节，管理员发放物资后，进入单据单元
		//shenQing.setHuanJie(huanJieManager.get(107));
		shenQing.setHuanJie(huanJieManager.get(108));
		shenQingManager.cover(shenQing);
		wuZi.setBeiZhu(beiZhu);
		User shiYongR=userManager.getUser(shiYonguserId);
		wuZi.setUser(shiYongR);
		wuZi.setDep(depManager.get(shiYongdepId));
		wuZiManager.cover(wuZi);
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJL.setHuanJie(huanJieManager.get(106));
		shenQingJL.setPiFuLX("通过");
		shenQingJL.setPiFuNR("物资发放");
		shenQingJL.setShenQing(shenQing);		
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		shenQingJL.setUser(user);
		shenQingJLManager.save(shenQingJL);

		//使用人接收
		toDoManager.deleteList(shenQing);
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 使用人领用
	 */
	@RequestMapping(value = "/wzlyList")
	public ModelAndView wzlyList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("caiGouLingYong", shenQingManager.getAllLingYong(user));
		mav.setViewName("shenqing/wzlyListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/caiGouJieShou")
	public void caiGouJieShou(String id,HttpServletResponse response){
		ShenQing shenQing=shenQingManager.get(id);
		shenQing.setHuanJie(huanJieManager.get(108));
		shenQingManager.cover(shenQing);
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJL.setHuanJie(huanJieManager.get(107));
		shenQingJL.setPiFuLX("通过");
		shenQingJL.setPiFuNR("使用人验收");		
		shenQingJL.setShenQing(shenQing);
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		shenQingJL.setUser(user);
		shenQingJLManager.save(shenQingJL);
		WuZi wuZi=shenQing.getWuZi();
		wuZi.setChuKuRQ(new Date());
		wuZi.setShiYongZT(shiYongZTManager.get(102));
		wuZiManager.cover(wuZi);
		
		//使用人已接收
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		toDoManager.deleteList(shenQing);
		for(User user1:userManager.getGZGLY()){
			ToDo toDo = new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(108));
			toDo.setShenQing(shenQing);
			toDo.setTask("使用人已验收");
			toDo.setType("申请采购");
			toDo.setUser(user1);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(108);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 108, user1, youJian);
				st.setWuZi(wuZi);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/shiYongRTH")
	public ModelAndView shiYongRTH(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("shenQingId", id);
		mav.setViewName("shenqing/shiYongRTH");
		return mav;
	}
	
	@RequestMapping(value = "/shiYongRTHSave")
	public void shiYongRTHSave(String shenQingId,String tuiHuiYY,HttpServletResponse response){
		ShenQing shenQing=shenQingManager.get(shenQingId);
		shenQing.setHuanJie(huanJieManager.get(106));
		shenQingManager.cover(shenQing);
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJL.setHuanJie(huanJieManager.get(107));
		shenQingJL.setPiFuLX(tuiHuiYY);
		shenQingJL.setPiFuNR("申请人退回");		
		shenQingJL.setShenQing(shenQing);
		shenQingJL.setUser(user);
		shenQingJLManager.save(shenQingJL);
		WuZi wuZi=shenQing.getWuZi();
		wuZi.setShiYongZT(shiYongZTManager.get(101));
		wuZiManager.cover(wuZi);
		
		//使用人已接收
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		toDoManager.deleteList(shenQing);
		for(User user1:userManager.getGZGLY()){
			ToDo toDo = new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(106));
			toDo.setShenQing(shenQing);
			toDo.setTask("验收入库");
			toDo.setType("申请采购");
			toDo.setUser(user1);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(106);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 106, user1, youJian);
				st.setWuZi(wuZi);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/wzlyPL")
	public void wzlyPL(HttpServletRequest request,HttpServletResponse response){
		String[] type = request.getParameterValues("wzlyId");
		String result="{\"result\":\"none\"}";
		if(type!=null){
			result="{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/wzlyPLSave")
	public void wzlyPLSave(HttpServletRequest request,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		String type[]=request.getParameterValues("wzlyId");
		if(type!=null){
			for(int i=0;i<type.length;i++){
				ShenQing shenQing=shenQingManager.get(type[i]);
				shenQing.setHuanJie(huanJieManager.get(108));
				shenQingManager.cover(shenQing);
				ShenQingJL shenQingJL=new ShenQingJL();
				shenQingJL.setCaoZuoRQ(new Date());
				shenQingJL.setHuanJie(huanJieManager.get(107));
				shenQingJL.setPiFuLX("通过");
				shenQingJL.setPiFuNR("申请人验收");		
				shenQingJL.setShenQing(shenQing);
				shenQingJL.setUser(user);
				shenQingJLManager.save(shenQingJL);
				WuZi wuZi=shenQing.getWuZi();
				wuZi.setChuKuRQ(new Date());
				wuZi.setShiYongZT(shiYongZTManager.get(102));
				wuZiManager.cover(wuZi);		
				//使用人已接收
				toDoManager.deleteList(shenQing);
				for(User user1:userManager.getGZGLY()){
					ToDo toDo = new ToDo();
					toDo.setCreateDate(new Date());
					toDo.setHuanJie(huanJieManager.get(108));
					toDo.setShenQing(shenQing);
					toDo.setTask("使用人已验收");
					toDo.setType("申请采购");
					toDo.setUser(user1);
					toDoManager.save(toDo);
					//\\发送邮件
					YouJian youJian=youJianManager.get(108);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 108, user1, youJian);
						st.setWuZi(wuZi);
						Thread t=new Thread(st);
						t.start();
					}
				}
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/wzlyPLTH")
	public ModelAndView wzlyPLTH(HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		String[] type = request.getParameterValues("wzlyId");
		if(type!=null){
			String wzlyIds="";
			for(int i=0;i<type.length;i++){
				wzlyIds=wzlyIds+type[i]+",";
			}
			if(wzlyIds.length()>0)
				wzlyIds=wzlyIds.substring(0, wzlyIds.length()-1);
			mav.addObject("wzlyIds", wzlyIds);
		}
		mav.setViewName("shenqing/wzlyPLTH");
		return mav;
	}
	
	@RequestMapping(value = "/wzlyPLTHSave")
	public void wzlyPLTHSave(String wzlyIds,String tuiHuiYY,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String result="{\"result\":\"success\"}";
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		if(wzlyIds!=null && !wzlyIds.equals("")){
			String wzlyId[]=wzlyIds.split(",");
			for(int i=0;i<wzlyId.length;i++){
				ShenQing shenQing=shenQingManager.get(wzlyId[i]);
				shenQing.setHuanJie(huanJieManager.get(106));
				shenQingManager.cover(shenQing);
				ShenQingJL shenQingJL=new ShenQingJL();
				shenQingJL.setCaoZuoRQ(new Date());
				shenQingJL.setHuanJie(huanJieManager.get(107));
				shenQingJL.setPiFuLX(tuiHuiYY);
				shenQingJL.setPiFuNR("申请人退回");		
				shenQingJL.setShenQing(shenQing);
				shenQingJL.setUser(user);
				shenQingJLManager.save(shenQingJL);
				WuZi wuZi=shenQing.getWuZi();
				wuZi.setShiYongZT(shiYongZTManager.get(101));
				wuZiManager.cover(wuZi);
				
				//使用人已接收
				toDoManager.deleteList(shenQing);
				for(User user1:userManager.getGZGLY()){
					ToDo toDo = new ToDo();
					toDo.setCreateDate(new Date());
					toDo.setHuanJie(huanJieManager.get(106));
					toDo.setShenQing(shenQing);
					toDo.setTask("验收入库");
					toDo.setType("申请采购");
					toDo.setUser(user1);
					toDoManager.save(toDo);
					//\\发送邮件
					YouJian youJian=youJianManager.get(106);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 106, user1, youJian);
						st.setWuZi(wuZi);
						Thread t=new Thread(st);
						t.start();
					}
				}
			}
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 低值管理员打印单据
	 */
	@RequestMapping(value = "/syryysList")
	public ModelAndView syryysList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("caiGouRZ", shenQingManager.getAllCaiGouZT(108));
		mav.setViewName("shenqing/syryysListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/syryysPLDY")
	public void syryysPLDY(HttpServletRequest request,HttpServletResponse response){
		String[] type = request.getParameterValues("ysId");
		String result="{\"result\":\"none\"}";
		if(type!=null){
			String ysIds="";
			for(int i=0;i<type.length;i++){
				ysIds=ysIds+type[i]+",";
			}
			if(ysIds.length()>0)
				ysIds=ysIds.substring(0, ysIds.length()-1);
			result="{\"result\":\""+ysIds+"\"}";
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/jiaoYanCaiGouDaYin")
	public APIResult jiaoYanCaiGouDaYin(@RequestParam(name = "ids", required = true) List<String> ids) {
		int i = 1;
		String gys = null;
		for (String id : ids) {
			ShenQing shenQing = shenQingManager.get(id);
			WuZi wuZi = shenQing.getWuZi();
			String gongYingShangMC = wuZi.getGongYingShangMC();
			if (!StringUtils.hasText(gys)) {
				gys = gongYingShangMC;
			}
			if (!StringUtils.hasText(gongYingShangMC) || !gys.equals(gongYingShangMC)) {
				return APIResult.fail("供应商不一致");
			}
		}
		return APIResult.success();
	}

	private String mergeString(String[] text) {
		if (text == null || text.length <= 0) {
			return null;
		}

		String str = "";
		for (String t : text) {
			if (StringUtils.hasText(t)) {
				str += t + ";";
			}
		}
		if (!"".equals(str)) {
			str = str.substring(0, str.length());
		}
		return str;
	}

	private String[] getJineDX(String strJinE) {
		String afterJneE = strJinE.substring(strJinE.indexOf(".") + 1);;
		if (afterJneE.length() == 1) {
			strJinE += "0";
		}
		String[] jineDX = new String[10];
		String number = strJinE.replace(".", "");
		int index = jineDX.length - number.length();
		int j = 0;
		for (int i = 0; i < jineDX.length; i++) {
			if (i < index) {
				jineDX[i] = "";
				continue;
			}

			jineDX[i] = String.valueOf(number.charAt(j));
			j++;
		}
		return jineDX;
	}
	
	@RequestMapping(value = "/caiGouDaYinHTML")
	public ModelAndView caiGouDaYinHTML(@RequestParam(value = "ids", required = true) String ids){
		ModelAndView mav=new ModelAndView();

		Date gouMaiRQ = null;
		double zongJinE = 0L;
		String gysMC = null;
		String faPiaoHao = "";
		String yanShouR = "";
		String caiGouR = "";
		String gkbmfzr = "";
		WuZi wuZi = null;
		Map<String, CaiGouDaYinVO> map = new HashMap<>();

		CaiGouDaYinVO vo = null;
		String[] arryId = ids.split(",");
		for (String id : arryId) {
			ShenQing shenQing=shenQingManager.get(id);
			wuZi = shenQing.getWuZi();
			gouMaiRQ = wuZi.getGouMaiRQ();
			if (StringUtils.hasText(shenQing.getUser().getName()) && !yanShouR.contains(shenQing.getUser().getName())) {
				yanShouR += " " + shenQing.getUser().getName();
			}

			if (StringUtils.hasText(shenQing.getCaiMaiR().getName()) && !caiGouR.contains(shenQing.getCaiMaiR().getName())) {
				caiGouR += " " + shenQing.getCaiMaiR().getName();
			}

			String gongYingShangMC = wuZi.getGongYingShangMC();
			if (gysMC == null) {
				gysMC = gongYingShangMC;
			}

			// 不同供应商不允许合并
			if (!gysMC.equals(gongYingShangMC)) {
				continue;
			}

			List<User> gkbmfzrList = shenQingJLManager.listUser(id, 103);
			if (!CollectionUtils.isEmpty(gkbmfzrList)) {
				for (User gkr : gkbmfzrList) {
					if (!gkbmfzr.contains(gkr.getName())) {
						gkbmfzr += " " + gkr.getName();
					}
				}
			}

			double jinE = wuZi.getWuZiYZ();
			zongJinE = zongJinE + jinE;
			String strJinE = String.valueOf(jinE);

			// 同类型的物品（名称、规格、单价一样），数量加1
			String key = mergeString(new String[]{ wuZi.getWuZiMC(), wuZi.getGuiGeXH(), strJinE});
			if (map.containsKey(key)) {
				CaiGouDaYinVO cgd = map.get(key);
				int sl = cgd.getShenQing().getShenQingSL() + 1;
				cgd.getShenQing().setShenQingSL(sl);
				cgd.setJineDX(getJineDX(String.valueOf(jinE * sl)));
				continue;
			}

			// 不同类型的物品，加一行
			vo = new CaiGouDaYinVO();
			vo.setWuZi(wuZi);
			vo.setShenQing(shenQing);
			vo.setJineDX(getJineDX(strJinE));
			map.put(key, vo);
		}

		List<CaiGouDaYinVO> data = new ArrayList<>();
		for(Map.Entry<String, CaiGouDaYinVO> entry : map.entrySet()){
			data.add(entry.getValue());
		}

		if (wuZi != null) {
			List<CaiGou> caiGouList = caiGouManager.list(wuZi);
			if (!CollectionUtils.isEmpty(caiGouList)) {
				CaiGou caiGou = caiGouList.get(0);
				faPiaoHao = caiGou.getFaPiaoHao();
			}
		}

		mav.addObject("data", data);
		mav.addObject("zongJinE", MoneyUtils.change(zongJinE));
		mav.addObject("gouMaiRQ", DateUtils.format(gouMaiRQ, FORMAT_SHORT_CN));
		mav.addObject("dangQianRQ", DateUtils.format(new Date(), FORMAT_SHORT_CN));
		mav.addObject("gongYingShangMC", gysMC);
		mav.addObject("faPiaoHao", faPiaoHao);
		mav.addObject("yanShouR", yanShouR);
		mav.addObject("caiGouR", caiGouR);
		mav.addObject("gkmbfzr", gkbmfzr);
		mav.setViewName("shenqing/caiGouDYHTML");
		return mav;
	}
	
	@RequestMapping(value = "/syryysPLRZ")
	public void syryysPLRZ(HttpServletRequest request,HttpServletResponse response){
		String[] type = request.getParameterValues("ysId");
		String result="{\"result\":\"none\"}";
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		if(type!=null){
			for(int i=0;i<type.length;i++){
				ShenQing shenQing=shenQingManager.get(type[i]);
				shenQing.setHuanJie(huanJieManager.get(109));
				shenQingManager.cover(shenQing);
				ShenQingJL shenQingJL=new ShenQingJL();
				shenQingJL.setCaoZuoRQ(new Date());
				shenQingJL.setHuanJie(huanJieManager.get(108));
				shenQingJL.setPiFuLX("通过");
				shenQingJL.setPiFuNR("采购物资入账");
				shenQingJL.setShenQing(shenQing);
				shenQingJL.setUser(user);
				shenQingJLManager.save(shenQingJL);
				//报财务
				toDoManager.deleteList(shenQing);
			}
			result="{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/caiGouRZ")
	public void caiGouRZ(String id,HttpServletResponse response){
		ShenQing shenQing=shenQingManager.get(id);
		shenQing.setHuanJie(huanJieManager.get(109));
		//shenQing.setHuanJie(huanJieManager.get(110));
		shenQingManager.cover(shenQing);
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJL.setHuanJie(huanJieManager.get(108));
		shenQingJL.setPiFuLX("通过");
		shenQingJL.setPiFuNR("采购物资入账");
		shenQingJL.setShenQing(shenQing);
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		shenQingJL.setUser(user);
		shenQingJLManager.save(shenQingJL);

		toDoManager.deleteList(shenQing);


		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 财务确认列表
	 */
	@RequestMapping(value = "/cwysList")
	public ModelAndView cwysList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("caiGouCWYS", shenQingManager.getAllCaiGouZT(109));
		mav.setViewName("shenqing/cwysListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/caiGouCWQR")
	public ModelAndView caiGouCWQR(String id){
		ShenQing shenQing=shenQingManager.get(id);
		WuZi wuZi=shenQing.getWuZi();
		ModelAndView mav=new ModelAndView();
		mav.addObject("shenQingId", id);
		mav.addObject("wuZi", wuZi);
		String format="yyyy-MM-dd";
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		String dangQianRQ=sdf.format(new Date());
		mav.addObject("dangQianRQ", dangQianRQ);
		mav.setViewName("wuzi/ziChanBM");
		mav.addObject("depList", depManager.getAllDep());
		return mav;
	}

	@Deprecated
	@RequestMapping(value = "/caiGouCWQRSave")
	public void caiGouCWQRSave(String shenQingId,WuZi temp,String guanLiBMId,String kaiShiJiTiRQStr,String baoXiuJZRQStr,String zheJiuLX,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		ShenQing shenQing=shenQingManager.get(shenQingId);
		shenQing.setHuanJie(huanJieManager.get(110));
		shenQingManager.cover(shenQing);
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJL.setHuanJie(huanJieManager.get(109));
		shenQingJL.setPiFuLX("确定资产编码");
		shenQingJL.setPiFuNR("输入资产编码（财务）");
		shenQingJL.setShenQing(shenQing);
		shenQingJL.setUser(user);
		shenQingJLManager.save(shenQingJL);
		WuZi yuan=wuZiManager.get(temp.getId());
		yuan.setZiChanBM(temp.getZiChanBM());
		String format="yyyy-MM-dd";
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		try{
			yuan.setKaiShiJiTiRQ(sdf.parse(kaiShiJiTiRQStr));
		}catch(Exception e){			
		}
		if(!temp.getWuZiMC().equals(yuan.getWuZiMC())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("物资名称");
			xiuGaiJL.setYuanZhi(yuan.getWuZiMC());
			xiuGaiJL.setXinZhi(temp.getWuZiMC());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setWuZiMC(temp.getWuZiMC());
		}
		if(yuan.getGuanLiBM()==null&&!guanLiBMId.equals("")){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("管理部门");
			xiuGaiJL.setYuanZhi("");
			xiuGaiJL.setXinZhi(depManager.get(guanLiBMId).getDepName());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setGuanLiBM(depManager.get(guanLiBMId));
		}else if(!guanLiBMId.equals(yuan.getGuanLiBM().getId())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("管理部门");
			xiuGaiJL.setYuanZhi(yuan.getGuanLiBM().getDepName());
			xiuGaiJL.setXinZhi(depManager.get(guanLiBMId).getDepName());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setGuanLiBM(depManager.get(guanLiBMId));
		}
		if(!temp.getPinPai().equals(yuan.getPinPai())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("物资品牌");
			xiuGaiJL.setYuanZhi(yuan.getPinPai());
			xiuGaiJL.setXinZhi(temp.getPinPai());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setPinPai(temp.getPinPai());
		}
		if(!temp.getZiChanBM().equals(yuan.getZiChanBM())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("资产编码");
			xiuGaiJL.setYuanZhi(yuan.getZiChanBM());
			xiuGaiJL.setXinZhi(temp.getZiChanBM());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setZiChanBM(temp.getZiChanBM());
		}
		if(!temp.getGuiGeXH().equals(yuan.getGuiGeXH())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("规格型号");
			xiuGaiJL.setYuanZhi(yuan.getGuiGeXH());
			xiuGaiJL.setXinZhi(temp.getGuiGeXH());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setGuiGeXH(temp.getGuiGeXH());
		}

		if(temp.getWuZiYZ()!=yuan.getWuZiYZ()){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("物资原值");
			xiuGaiJL.setYuanZhi(Double.toString(yuan.getWuZiYZ()));
			xiuGaiJL.setXinZhi(Double.toString(temp.getWuZiYZ()));
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setWuZiYZ(temp.getWuZiYZ());
		}if(temp.getWuZiJingZhi()!=yuan.getWuZiJingZhi()){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("物资净值");
			xiuGaiJL.setYuanZhi(Double.toString(yuan.getWuZiJingZhi()));
			xiuGaiJL.setXinZhi(Double.toString(temp.getWuZiJingZhi()));
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setWuZiJingZhi(temp.getWuZiJingZhi());
		}
		if(temp.getJingCanZhiLv()!=yuan.getJingCanZhiLv()){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("净残值率");
			xiuGaiJL.setYuanZhi(Float.toString(yuan.getJingCanZhiLv()));
			xiuGaiJL.setXinZhi(Float.toString(temp.getJingCanZhiLv()));
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setJingCanZhiLv(temp.getJingCanZhiLv());
		}
		if(!temp.getShengChanCJ().equals(yuan.getShengChanCJ())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("生产厂家");
			xiuGaiJL.setYuanZhi(yuan.getShengChanCJ());
			xiuGaiJL.setXinZhi(temp.getShengChanCJ());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setShengChanCJ(temp.getShengChanCJ());
		}
		if(!temp.getGongYingShangMC().equals(yuan.getGongYingShangMC())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("供应商名称");
			xiuGaiJL.setYuanZhi(yuan.getGongYingShangMC());
			xiuGaiJL.setXinZhi(temp.getGongYingShangMC());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setGongYingShangMC(temp.getGongYingShangMC());
		}



		try{
		    temp.setBaoXiuJZRQ(sdf.parse(baoXiuJZRQStr));
		}catch(Exception e){
		}
		if(yuan.getBaoXiuJZRQ()==null && temp.getBaoXiuJZRQ()!=null){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("保修截止日期");
			xiuGaiJL.setXinZhi(sdf.format(temp.getBaoXiuJZRQ()));
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setBaoXiuJZRQ(temp.getBaoXiuJZRQ());
		}else if(yuan.getBaoXiuJZRQ()==null && temp.getBaoXiuJZRQ()==null){
			
		}else if(!sdf.format(temp.getBaoXiuJZRQ()).equals(sdf.format(yuan.getBaoXiuJZRQ()))){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("保修截止日期");
			xiuGaiJL.setYuanZhi(sdf.format(yuan.getBaoXiuJZRQ()));
			xiuGaiJL.setXinZhi(sdf.format(temp.getBaoXiuJZRQ()));
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setBaoXiuJZRQ(temp.getBaoXiuJZRQ());
		}

		if(!temp.getCunFangD().equals(yuan.getCunFangD())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("存放地点");
			xiuGaiJL.setYuanZhi(yuan.getCunFangD());
			xiuGaiJL.setXinZhi(temp.getCunFangD());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setCunFangD(temp.getCunFangD());
		}
		yuan.setBeiZhu(temp.getBeiZhu());
		wuZiManager.cover(yuan);
		toDoManager.deleteList(shenQing);
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 其他
	 */
	@RequestMapping(value = "/shenQingDetail")
	public ModelAndView shenQingDetail(String id){
		ShenQing shenQing=shenQingManager.get(id);
		ModelAndView mav=new ModelAndView();
		mav.addObject("shenQing", shenQing);
		mav.addObject("xiaYiBuR", getXiaYiBuR(id));
		mav.addObject("shenQingJLList", shenQingJLManager.getAllJL(id));		
		mav.setViewName("shenqing/shenQingDetail");
		return mav;
	}
	
	@RequestMapping(value = "/myShenQingSP")
	public ModelAndView myShenQingSP(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("shenQingSPList", shenQingJLManager.getMySPJL(user));
		mav.setViewName("shenqing/myShenQingSP");
		return mav;
	}
	
	@RequestMapping(value = "/shenQingCheHui", method = RequestMethod.POST)
	@ResponseBody
	public APIResult shenQingCheHui(String id,HttpServletResponse response){
		ShenQing shenQing=shenQingManager.get(id);
		int dangQian=shenQing.getHuanJie().getId();
		shenQingManager.cheHui(id);

		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJL.setHuanJie(huanJieManager.get(dangQian));
		shenQingJL.setPiFuLX("撤回申请");
		shenQingJL.setPiFuNR("撤回申请");
		shenQingJL.setShenQing(shenQing);
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		shenQingJL.setUser(user);
		shenQingJLManager.save(shenQingJL);

		toDoManager.deleteList(shenQing);
		return APIResult.success();
	}
}
